// pages/common/calendar/calendar.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule:1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '选择时间', // 导航栏 中间的标题
      textColor: '#FFFFFF', // 标题颜色
      bgColor: '#F76A1A', // 导航栏背景颜色
      isloading: false,
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.systeminfo.statusBarHeight * 2 + 20,
    hasEmptyGrid: false,
    cur_year: '',
    cur_month: '',
    start: '',
    end: '',
    timeArr:[],
    moneyArr:[],
    yearArr:[],
    start_month: '',
    end_month:'',
    start_year: '',
    end_year:'',
    startTime: '',
    endTime: '',
    loadText: '',
    path_url: '',

    istong: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNowDate();
    this.setData({
      path_url: options.path_url
    })
  },
  formatting(num){
    if(num < 9){
      var arr=['00','01','02','03','04','05','06','07','08','09'];
      console.log("cur_day",arr[num+1])
      return arr[num+1] 
    }else{
      return num+1
    }
  },
  formattingmonth(num){
    if(num < 9){
      var arr=['00','01','02','03','04','05','06','07','08','09'];
      console.log("cur_day",arr[num])
      return arr[num] 
    }else{
      return num
    }
  },
  dateSelectAction: function (e) {
    var startTime =  '';
    var endTime = '';
    var cur_day = e.currentTarget.dataset.idx;
    var sel_month = e.currentTarget.dataset.month;
    var cur_month = this.data.cur_month
    var cur_month_zero = this.formattingmonth(cur_month);
    var cur_year = this.data.cur_year
    var cur_day_zero = this.formatting(cur_day);
    var timeArr = this.data.timeArr;
    var moneyArr = this.data.moneyArr;
    var yearArr =  this.data.yearArr;
    timeArr.push(cur_day)
    moneyArr.push(cur_month)
    yearArr.push(this.data.cur_year)
    if(timeArr.length > 2){
      timeArr = [];
      moneyArr = [];
      yearArr = [],
      timeArr.push(cur_day)
      moneyArr.push(cur_month)
      yearArr.push(this.data.cur_year)
      startTime = `${this.data.cur_year}-${cur_month_zero}-${cur_day_zero}`;
      console.log("startTime",startTime)
      this.setData({
        timeArr: timeArr,
        moneyArr: moneyArr,
        yearArr: yearArr,
        start: timeArr[0],
        start_month: moneyArr[0],
        start_year: yearArr[0],
        startTime: startTime,
        end: ''
      })
    }else if(timeArr.length === 1){
      console.log("第一次点日",timeArr[0])
      console.log("第一次点月","++++++",moneyArr[0])
      console.log("第一次点年","++++++",yearArr[0])
      startTime = `${this.data.cur_year}-${cur_month_zero}-${cur_day_zero}`;

       this.setData({
         start: timeArr[0],
         start_month: moneyArr[0],
         start_year: yearArr[0],
         startTime: startTime,
         todayIndex: cur_day,
       })
    }else if(timeArr.length === 2){
      console.log("第二次点日",timeArr[1],"++++++",timeArr[0])
      console.log("第二次点月",moneyArr[1],"++++++",moneyArr[0])
      console.log("第二次点年",yearArr[1],"++++++",yearArr[0])
      startTime = `${this.data.cur_year}-${cur_month_zero}-${cur_day_zero}`;
      console.log("startTime",startTime)
      if(yearArr[1] < yearArr[0]){
        console.log("年数大")
        timeArr = [];
        moneyArr = [];
        yearArr = [];
        timeArr.push(cur_day)
        moneyArr.push(this.data.cur_month)
        yearArr.push(this.data.cur_year)
        this.setData({
          timeArr: timeArr,
          moneyArr: moneyArr,
          yearArr: yearArr,
          start: cur_day,
          start_month:cur_month,
          start_year: cur_year,
          startTime: startTime,
          todayIndex: cur_day,
        })
        return false
      }
      if(moneyArr[1] < moneyArr[0] && yearArr[1] < yearArr[0]){
        console.log("月数大")
        timeArr = [];
        moneyArr = [];
        timeArr.push(cur_day)
        moneyArr.push(this.data.cur_month)
        this.setData({
          timeArr: timeArr,
          moneyArr: moneyArr,
          start: cur_day,
          start_month: cur_month,
          start_year: cur_year,
          startTime: startTime,
          todayIndex: cur_day,
        })
        return false
      }
      if(moneyArr[1] < moneyArr[0] && yearArr[1] == yearArr[0]){
        console.log("月数大")
        timeArr = [];
        moneyArr = [];
        timeArr.push(cur_day)
        moneyArr.push(this.data.cur_month)
        this.setData({
          timeArr: timeArr,
          moneyArr: moneyArr,
          start: cur_day,
          start_month: cur_month,
          start_year: cur_year,
          startTime: startTime,
          todayIndex: cur_day,
        })
        return false
      }
      if(timeArr[1] <  timeArr[0] && moneyArr[1] < moneyArr[0] && yearArr[1] < yearArr[0]){
        console.log("天数大")
        timeArr = [];
        timeArr.push(cur_day)
        this.setData({
          timeArr: timeArr,
          start: cur_day,
          startTime: startTime,
          todayIndex: cur_day,
        })
        return false
      }
      if(timeArr[1] <  timeArr[0] && moneyArr[1] == moneyArr[0] || yearArr[1] < yearArr[0]){
        console.log("天数大")
        timeArr = [];
        timeArr.push(cur_day)
        this.setData({
          timeArr: timeArr,
          start: cur_day,
          startTime: startTime,
          todayIndex: cur_day,
        })
        return false
      }
      endTime = `${this.data.cur_year}-${cur_month_zero}-${cur_day_zero}`
      this.setData({
        end: timeArr[1],
        end_month: moneyArr[1],
        end_year: yearArr[1],
        endTime: endTime,
        todayIndex: cur_day,
      })
   }
    console.log(startTime,"+++",endTime)
    console.log(this.data.timeArr)
    console.log(`点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day_zero}日`);
 },

 setNowDate: function () {
   const date = new Date();
   const cur_year = date.getFullYear();
   const cur_month = date.getMonth() + 1;
   const todayIndex = date.getDate() - 1;
   console.log(`日期：${todayIndex}`)
   const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
   this.calculateEmptyGrids(cur_year, cur_month);
   this.calculateDays(cur_year, cur_month);
   this.setData({
     cur_year: cur_year,
     cur_month: cur_month,
     start: todayIndex,
     start_month: cur_month,
     start_year: cur_year,
     weeks_ch,
     
   })
 },

 getThisMonthDays(year, month) {
   return new Date(year, month, 0).getDate();
 },
 getFirstDayOfWeek(year, month) {
   return new Date(Date.UTC(year, month - 1, 1)).getDay();
 },
 calculateEmptyGrids(year, month) {
   const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
   let empytGrids = [];
   if (firstDayOfWeek > 0) {
     for (let i = 0; i < firstDayOfWeek; i++) {
       empytGrids.push(i);
     }
     this.setData({
       hasEmptyGrid: true,
       empytGrids
     });
   } else {
     this.setData({
       hasEmptyGrid: false,
       empytGrids: []
     });
   }
 },
 calculateDays(year, month) {
   let days = [];

   const thisMonthDays = this.getThisMonthDays(year, month);

   for (let i = 1; i <= thisMonthDays; i++) {
     days.push(i);
   }

   this.setData({
     days
   });
 },
 handleCalendar(e) {
   const handle = e.currentTarget.dataset.handle;
   const cur_year = this.data.cur_year;
   const cur_month = this.data.cur_month;
   var timeArr = this.data.timeArr;
   if (handle === 'prev') {
     let newMonth = cur_month - 1;
     let newYear = cur_year;
     if (newMonth < 1) {
       newYear = cur_year - 1;
       newMonth = 12;
     }
    if(timeArr.length > 2){
      this.setData({
        timeArr: [],
        moneyArr: [],
        yearArr: [],
      })
    }
     this.calculateDays(newYear, newMonth);
     this.calculateEmptyGrids(newYear, newMonth);

     this.setData({
       cur_year: newYear,
       cur_month: newMonth
     })
   } else {
     let newMonth = cur_month + 1;
     let newYear = cur_year;
     if (newMonth > 12) {
       newYear = cur_year + 1;
       newMonth = 1;
     }
     if(timeArr.length > 2){
      this.setData({
        timeArr: [],
        moneyArr: [],
        yearArr: [],
      })
    }
     this.calculateDays(newYear, newMonth);
     this.calculateEmptyGrids(newYear, newMonth);

     this.setData({
       cur_year: newYear,
       cur_month: newMonth
     })
   }
   console.log("cur_month",this.data.cur_month)
   console.log("start_month",this.data.start_month)
 },
  confirmTime(){
    var startTime = this.data.startTime;
    var endTime = this.data.endTime
    var seleTime = [];
    if(startTime === '' || endTime === ''){
      this.showDialog();
      this.setData({
        loadText: '请选择时间段'
      })
      return false
    }
    seleTime.push(startTime);
    seleTime.push(endTime); 
    console.log("seleTime",seleTime)
    this.handleClick(seleTime)
  },
  handleClick(seleTime){
    console.log("seleTime",seleTime)
    let pages = getCurrentPages();   //获取小程序页面栈
    let beforePage = pages[pages.length -2];  //获取上个页面的实例对象
    console.log("过来的路径",beforePage)
    beforePage.setData({      //直接修改上个页面的数据（可通过这种方式直接传递参数）
      timeArr: seleTime
    })
    if(beforePage.route =='userPageA/pages/userMymoney/exchangeBill/exchangeBill'){
      beforePage.formattingTime();
      beforePage.getverificationList()
    } 
    else if(beforePage.route =='merchantsPageA/pages/paymentCode/collectionBill/collectionBill'){
      beforePage.formattingTime();
      beforePage.getreceivingBill();   //触发上个页面自定义的go_update方法
      
    }else if(beforePage.route =='merchantsPageA/pages/verificationDeta/verificationDeta'){
      beforePage.formattingTime();
      beforePage.getverificationList()
    }else if(beforePage.route == 'pages/balanceDeta/balanceDeta'){
      beforePage.formattingTime();
      beforePage.getData()
    }
   
    wx.navigateBack({         //返回上一页  
      delta:1
    })
  },
  onReady: function () {
    this.smallLay = this.selectComponent("#smallLay"); 
  },
  showDialog() {
    this.smallLay.showSmallLay();
  },
})